﻿using System;
using System.Collections.Generic;
using System.Windows;
using SmartSpace.Workflow;
using SsTool.Common;

namespace SsTool.Workflow.Windows
{
    /// <summary>
    /// Interaction logic for CreateWorkflowMetadataWindow.xaml
    /// </summary>
    public partial class CreateWorkflowMetadataWindow : Window
    {
        private WorkflowMetadata _wfMetadata;

        public CreateWorkflowMetadataWindow()
        {
            InitializeComponent();

            _wfMetadata = new WorkflowMetadata();
            _wfMetadata.Id = Guid.NewGuid();
            _wfMetadata.Caption = string.Format("新工作流{0}", _wfMetadata.Id);
            this.DataContext = _wfMetadata;
            //_ctlWorkflowPanel.DoubleClick += new Action<ActivityMetadata>(_ctlWorkflowPanel_DoubleClick);
            this._ctlWorkflowPanel.ActivityPanelDoubleClick += new Action<ActivityMetadata>(_ctlWorkflowPanel_ActivityPanelDoubleClick);
            this._ctlWorkflowPanel.EventPanelDoubleClick += new Action<EventMetadata>(_ctlWorkflowPanel_EventPanelDoubleClick);
        }

        public CreateWorkflowMetadataWindow(WorkflowMetadata wfMetadata)
        {
            InitializeComponent();

            _wfMetadata = wfMetadata;
            this.DataContext = _wfMetadata;

            WorkflowDiagram layout = App.Get<IWorkflowService>().FindDiagram(wfMetadata.Id);
            _ctlWorkflowPanel.Init(_wfMetadata, layout);

            //_ctlWorkflowPanel.DoubleClick += new Action<ActivityMetadata>(_ctlWorkflowPanel_DoubleClick);
            this._ctlWorkflowPanel.ActivityPanelDoubleClick += new Action<ActivityMetadata>(_ctlWorkflowPanel_ActivityPanelDoubleClick);
            this._ctlWorkflowPanel.EventPanelDoubleClick += new Action<EventMetadata>(_ctlWorkflowPanel_EventPanelDoubleClick);
        }

        private void _ctlBtnCreateActivity_Click(object sender, RoutedEventArgs e)
        {
            ActivityMetadata activity = new ActivityMetadata();
            activity.Id = Guid.NewGuid();
            activity.Caption = string.Format("活动{0}", activity.Id);
            activity.Workflow = this.DataContext as WorkflowMetadata;
            activity.WorkflowId = activity.Workflow.Id;
            if (_ctlWorkflowPanel.SelectedEvent != null)
            {
                //_ctlWorkflowPanel.SelectedActivity.NextActivities.Add(activity);
                _ctlWorkflowPanel.SelectedEvent.ToActivity = activity;
                _ctlWorkflowPanel.SelectedEvent.ToActivityId = activity.Id;

                _ctlWorkflowPanel.Add(activity);

                activity.IsRoot = false;
                _wfMetadata.Activities.Add(activity);
            }
            else if (_wfMetadata.GetRoot() == null)
            {
                activity.IsRoot = true;
                _wfMetadata.Activities = new List<ActivityMetadata>();
                _wfMetadata.Activities.Add(activity);

                _ctlWorkflowPanel.Add(activity);
            }
        }

        private void _ctlBtnCreateEvent_Click(object sender, RoutedEventArgs e)
        {
            if (_ctlWorkflowPanel.SelectedActivity != null)
            {
                UpdateEventWindow window = new UpdateEventWindow(
                    _ctlWorkflowPanel.AllActivities,
                    _ctlWorkflowPanel.SelectedActivity);
                if (window.ShowDialog() == true)
                {
                    EventMetadata @event = window.DataContext as EventMetadata;

                    if (_ctlWorkflowPanel.SelectedActivity.Events == null)
                    {
                        _ctlWorkflowPanel.SelectedActivity.Events = new List<EventMetadata>();
                    }

                    _ctlWorkflowPanel.SelectedActivity.Events.Add(@event);
                    _ctlWorkflowPanel.AddEvent(@event);
                }
            }

            //_ctlWorkflowPanel.UpdateLayout();

        }

        void _ctlWorkflowPanel_DoubleClick(ActivityMetadata obj)
        {
            UpdateActivityWindow window = new UpdateActivityWindow(obj);
            window.ShowDialog();
        }

        void _ctlWorkflowPanel_ActivityPanelDoubleClick(ActivityMetadata obj)
        {
            UpdateActivityWindow window = new UpdateActivityWindow(obj);
            window.ShowDialog();
        }

        void _ctlWorkflowPanel_EventPanelDoubleClick(EventMetadata obj)
        {
            UpdateEventWindow window = new UpdateEventWindow(_ctlWorkflowPanel.AllActivities, obj);
            if (window.ShowDialog() == true)
            {
                _ctlWorkflowPanel.AddLine();
            }
        }

        private void _ctlBtnSubmit_Click(object sender, RoutedEventArgs e)
        {
            IWorkflowService service = App.Get<IWorkflowService>();
            WorkflowDiagram diagram = _ctlWorkflowPanel.GetWorkflowLayout(this.DataContext as WorkflowMetadata);
            if (service.StoreWorkflow(this.DataContext as WorkflowMetadata))
            {
                service.StoreDiagram(diagram);
                DialogResult = true;
            }
        }

        private void _ctlBtnBrowse_Click(object sender, RoutedEventArgs e)
        {
            SelectEntityMetadataWindow window = new SelectEntityMetadataWindow();
            if (window.ShowDialog() == true)
            {
                (this.DataContext as WorkflowMetadata).Entity = window.SelectedEntity;
                (this.DataContext as WorkflowMetadata).EntityId = window.SelectedEntity.Id;
                this.DataContext = null;
                this.DataContext = _wfMetadata;
            }
        }
    }
}
